package serviceImpl;

import bean.UserInfo;
import bean.UserInfor;
import dao.UserDAO;
import impl.UserDaoImpl;
import service.UserInfoService;
import util.FileUtil;
import util.MD5Util;
import util.ServerResponseResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @classname:UserInfoServiceImpl
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/29 22:49
 * @version: 0.1
 * @since:1.8
 */
public class UserInfoServiceImpl implements UserInfoService {
    @Override
    public Map<String, Object> addUser(UserInfo userInfo) {

        //密码加密
        String pass = userInfo.getPass();
        String s = MD5Util.passMd5(pass);
        userInfo.setPass(s);
        //文件上传
        String image = userInfo.getImage();
        String imagePath = FileUtil.fileUpload(image);
        userInfo.setImage(imagePath);


        UserDAO userDAO = new UserDaoImpl();
        int i = userDAO.addUserParams(userInfo);
        Map<String, Object> map = new HashMap<>(16);
        if (i == 0) {
            map.put("code", 500);
            map.put("msg", "error");
            return map;
        }
        map.put("code", 200);
        map.put("msg", "success");
        return map;
    }

    @Override
    public Map<String, Object> selectAll() {
        UserDAO userDAO = new UserDaoImpl();
        List<UserInfor> list = userDAO.selectAll();
        Map<String, Object> map = new HashMap<>(16);
        if (!list.isEmpty()) {
            map.put("code", 500);
            map.put("msg", "error");
            return map;
        }
        map.put("code", 200);
        map.put("msg", "success");
        map.put("data",list);
        return map;


    }

    @Override
    public ServerResponseResult<List<UserInfor>> selectAllResponse() {
        UserDAO userDAO = new UserDaoImpl();
        List<UserInfor> list = userDAO.selectAll();

        if (!list.isEmpty()) {
            return ServerResponseResult.error();
        }
        return ServerResponseResult.success(list);
    }

}
